package cn.gz.demo.ui.xd;


import android.os.Bundle;
import android.os.Handler;
import android.widget.Toast;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.donkingliang.groupedadapter.adapter.GroupedRecyclerViewAdapter;
import com.donkingliang.groupedadapter.holder.BaseViewHolder;

import cn.gz.demo.R;
import cn.gz.demo.adapter.ExpandableAdapter;
import cn.gz.demo.databinding.RecylerViewXdBinding;
import cn.gz.demo.ui.base.BaseActivity;
import cn.gz.demo.view.GroupModel;
import cn.gz.demo.view.TopLayoutManager;

/**
 * Created by Liuyz on 2019/10/31.
 */

public class RecylerViewXdActivity extends BaseActivity {
    private RecylerViewXdBinding binding;
    private LinearLayoutManager manager = null;
    private ExpandableAdapter adapter;
    private int oldGroupPosition = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = DataBindingUtil.setContentView(this, R.layout.activity_recylerview_xd);

        initView();
    }

    private void initView() {
        binding.tvTitle.setText("可展开收起的列表");

        manager = new TopLayoutManager(context);
        binding.rvList.setLayoutManager(manager);
        adapter = new ExpandableAdapter(context, GroupModel.getExpandableGroups(10, 10));
        binding.rvList.setAdapter(adapter);

        adapter.setOnHeaderClickListener(new GroupedRecyclerViewAdapter.OnHeaderClickListener() {
            @Override
            public void onHeaderClick(GroupedRecyclerViewAdapter adapter, BaseViewHolder holder, final int groupPosition) {
                ExpandableAdapter expandableAdapter = (ExpandableAdapter) adapter;
                if (expandableAdapter.isExpand(groupPosition)) {
                    expandableAdapter.collapseGroup(groupPosition);
                } else {
                    if (oldGroupPosition >= 0) {
                        expandableAdapter.collapseGroup(oldGroupPosition);
                    }
                    expandableAdapter.expandGroup(groupPosition);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            binding.rvList.smoothScrollToPosition(groupPosition);
                        }
                    }, 500);
                }
                oldGroupPosition = groupPosition;
            }
        });
        adapter.setOnChildClickListener(new GroupedRecyclerViewAdapter.OnChildClickListener() {
            @Override
            public void onChildClick(GroupedRecyclerViewAdapter adapter, BaseViewHolder holder,
                                     int groupPosition, int childPosition) {
                Toast.makeText(RecylerViewXdActivity.this, "子项：groupPosition = " + groupPosition
                                + ", childPosition = " + childPosition,
                        Toast.LENGTH_LONG).show();
            }
        });
    }
}
